package com.hw.one.collect.util;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;

/**
 * Created by Administrator on 2017/8/23.
 * 序列化对象方法类
 *
 */
public class SerializeUtil {

    /**
     *
     * @CreateUser:
     * @ReturnType:byte[]
     * @param obj
     * @return
     * @CreateDate:2014-7-19上午11:38:19
     * @SerializeUtilUseFor :序列化一个对象
     */
    public static byte[] ObjTOSerialize(Object obj) {
        ObjectOutputStream oos = null;
        ByteArrayOutputStream byteOut = null;
        try {
            byteOut = new ByteArrayOutputStream();
            oos = new ObjectOutputStream(byteOut);
            oos.writeObject(obj);
            byte[] bytes = byteOut.toByteArray();
            return bytes;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     *
     * @CreateUser:Test
     * @ReturnType:Object
     * @param bytes
     * @return
     * @CreateDate:2014-7-19上午11:38:29
     * @SerializeUtilUseFor :反序列化
     */
    public static Object unSerialize(byte[] bytes) {
        ByteArrayInputStream in = null;
        try {
            in = new ByteArrayInputStream(bytes);
            ObjectInputStream objIn = new ObjectInputStream(in);
            return objIn.readObject();
        } catch (Exception e) {
        }
        return null;
    }
}
